package io.github.elkan1788.mpservice.module;

import io.github.elkan1788.mpsdk4j.core.WechatDefHandler;
import io.github.elkan1788.mpsdk4j.mvc.WechatWebSupport;
import io.github.elkan1788.mpsdk4j.vo.MPAccount;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.random.R;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@At("/mpdev")
@IocBean
public class WechatDevModule extends WechatWebSupport {

    private static final Log log = Logs.get();

    @Override
    public void init() {
        log.info("====== Nutz环境 =======");
        MPAccount mpAct = new MPAccount();
        mpAct.setAppId(System.getenv("WECHAT_DEV_APPID"));
        mpAct.setAppSecret(System.getenv("WECHAT_DEV_APPSECRET"));
        mpAct.setToken(System.getenv("WECHAT_DEV_TOKEN"));
        mpAct.setAESKey(R.UU32());
        _wk.setMpAct(mpAct);
        _wk.setWechatHandler(new WechatDefHandler());
    }

    @At("/entrance")
    public void interEntrance(HttpServletRequest req, HttpServletResponse resp) {
        try {
            this.interact(req, resp);
        }
        catch (IOException e) {
            throw Lang.wrapThrow(e);
        }
    }
}
